package com.example.myretrofit2.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myretrofit2.Goods;
import com.example.myretrofit2.R;
import com.example.myretrofit2.adapter.GoodsListAdapter;
import com.example.myretrofit2.adapter.OnitemGoodsClickLinsener;
import com.example.myretrofit2.common.MyApplication;
import com.example.myretrofit2.presenter.GoodsPresenter;
import com.example.myretrofit2.presenter.MySubscriber;

import java.util.ArrayList;
import java.util.List;

@RequiresApi(api = Build.VERSION_CODES.M)
public class GoodsList extends AppCompatActivity implements View.OnScrollChangeListener {
    RecyclerView recyclerView;
    Goods goods;
    TextView priceOrder;
    ImageView titleBack;
    TextView searchKeyword;
    TextView newGoods;
    List<Goods> goodsList;
    int lastSize = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list);
        goods = new Goods();
        priceOrder = findViewById(R.id.price_order);
        titleBack = findViewById(R.id.title_back);
        recyclerView = findViewById(R.id.goods_list_re);
        searchKeyword = findViewById(R.id.search_keyword);
        recyclerView.setOnScrollChangeListener(this);
        newGoods = findViewById(R.id.newGoods);
        goodsList=new ArrayList<>();
        searchKeyword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(GoodsList.this, SearchActiviry.class);
                startActivity(intent);
            }
        });
        titleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        priceOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("price".equals(goods.getOrderType())) {
                    goods.setOrderType("price desc");
                } else {
                    goods.setOrderType("price");
                }
                initgoodsList();
            }
        });
        newGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (goods.getNewgoods() == 1) {
                    initgoodsList();
                } else {
                    goods.setNewgoods(1);
                    initgoodsList();
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = getIntent();
        int catagoryId = intent.getIntExtra("catagoryId", 0);
        String goodsName = intent.getStringExtra("goodsName");
        if (!TextUtils.isEmpty(goodsName)) {
            goods.setGoodname(goodsName);
            searchKeyword.setText(goodsName);
        }
        goods.setCategoryid(catagoryId);
        goods.setPage(0);
        goods.setCount(10);
        goods.setIndex(goods.getCount() * goods.getPage());
        initgoodsList();
    }

    private void initgoodsList() {
        GoodsPresenter.getGoods(goods, new MySubscriber<List<Goods>>(this) {
            @Override
            public void onNext(List<Goods> list) {
                if (list.size() > 0) {
                    goods.setDataCount(list.get(0).getDataCount());
                    goodsList.addAll(list);
                }
                if (lastSize > 0) {
                    recyclerView.scrollToPosition(lastSize);
                } else{
                    GoodsListAdapter adapter = new GoodsListAdapter(MyApplication.getContext(), goodsList);
                recyclerView.setAdapter(adapter);
                LinearLayoutManager layoutManager = new LinearLayoutManager(MyApplication.getContext());
                layoutManager.setOrientation(RecyclerView.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                adapter.setLinsener(new OnitemGoodsClickLinsener() {
                    @Override
                    public void onItemOrderClick(Goods goods) {
                        Intent intent = new Intent();
                        intent.setClass(MyApplication.getContext(), GoodsInfoActivity.class);
                        intent.putExtra("goodsId", goods.getId());
                        startActivity(intent);
                    }
                });
            }
            }
        });
    }

    @Override
    public void onScrollChange(View view, int i, int i1, int i2, int i3) {

        /**
         * canScrollVertically 参数为正1，表示手指向上滑动
         * 参数为正 -11，表示手指向下滑动
         */
        if (!recyclerView.canScrollVertically(1)) {
            Toast.makeText(this, "到底部了", Toast.LENGTH_SHORT).show();
            int page = goods.getDataCount() / goods.getCount();
            int yushu = goods.getDataCount() % goods.getCount();
            if (yushu > 0 && yushu < 10) {
                page += 1;
            }
            if (goods.getPage() + 1 < page) {
                goods.setPage(goods.getPage() + 1);
                goods.setIndex(goods.getCount() * goods.getPage());
                lastSize=goods.getIndex();
                initgoodsList();
            } else {
                Toast.makeText(this, "没有更多数据了", Toast.LENGTH_SHORT).show();
            }

        }

    }
}